# ##高阶函数
# 把函数作为参数传入，这样的函数称为高阶函数
# def func01(a, b):
#     print(f"{a} ... {b}")
#
#
# def func02(func, a, b):
#     print("strt func")
#     func(a, b)


# 把函数作为参数传入式，而不是为了调用它（就是把函数的返回值作为参数传入），所以不用加小圆括号
# func02(func01, 3, 4)


# ---------------------------------------------------------------------------------
# ##map()高阶函数 返回的是一个可迭代对象 --》 一一映射的关系
# 这个函数是为了批量处理像列表这样的一组数据进行处理
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list2 = [3, 4, 5, 6, 7]

# for i in range(len(list1)):
#     list1[i] = pow(list1[i], 2)
# print(list1)


# def func(item):
#     return item * item


# result = map(func, list1)
# 也可以使用匿名函数
# result = map(lambda item: item * item, list1)
# print(list(result))


# def func1(item1, item2):
#     return item1 * item2
#
#
# result = map(func1, list1, list2)
# result = map(lambda item1, item2: item1 * item2, list1, list2)
# print(list(result))

# # 保留两位小数输出
# b = [1.3423434, 12.341233232, 3.2323411]
# result = map(lambda item: "%.2f" % item, b)
# print(list(result))
#
# # 百分比输出，保留两位小数
# c = [0.023421, 0.00134234, 0.00013413]
# result = map(lambda item: "%.2f%%" % (item * 100), c)
# print(list(result))
#
# # 将a里的数字转换成字符串形式
# a = [1, 2, 3, 4]
# print(type(a[0]))
# result = map(lambda item: str(item), a)
# li = list(result)
# print(li, type(li[0]))
#
# # 输出d中字符串中每个字符对应的ascii
# d = 'span'
# result = map(lambda item: ord(item), d)
# print(list(result))
#
# # 对列表中的每个元素取绝对值
# f = [-2, -3, 0, 5, -6]
# result = map(lambda item: abs(item), f)
# print(list(result))


# --------------------------------------------------------------------------------------
# ##filter()
# 过滤
# # 取出10以内的奇数
# result = filter(lambda item: item % 2 == 1, range(10))
# print(list(result))
# # [1, 3, 5, 7, 9]
# result = map(lambda item: item % 2 == 1, range(10))
# print(list(result))
# # [False, True, False, True, False, True, False, True, False, True]
#
#
# # 过滤出1-100以内开平方根是整数的数
# result = filter(lambda item: item**0.5 % 2 == 0 or item**0.5 % 2 == 1, range(1, 101))
# print(list(result))
# result = filter(lambda item: (item**0.5).is_integer(), range(1, 101))
# print(list(result))
# # [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# # [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
#
# # 过滤出100-999以内的水仙花数
# result = filter(lambda item: pow(item // 100, 3) + pow(item % 100 // 10, 3) + pow(item % 10, 3) == item, range(100, 1000))
# print(list(result))
# # [153, 370, 371, 407]


# ---------------------------------------------------------------------------------------
# # ##sorted 排序
# a = [2, 5, 6, 1]
# print(sorted(a))
#
# b = [0, 1, 0, True, False]
# print(sorted(b))
#
# # 按绝对值进行排序
# c = [-1, 1, 1, 0, 100, -100, -70]
# print(sorted(c, key=abs))
# # key你排序的方式
#
# # 忽略字母大小写排序
# d = ['aaaaaaaaa', 'bob', 'about', 'Zoo', 'Credit']
# print(sorted(d, key=str.lower))
# # 根据字符串长度进行排序
# print(sorted(d, key=len))
#
# # 元组
# lst = [(1, 1), (1, 0), (2, 2), (2, 1), (True, False)]
# print(sorted(lst))
#
# # 字典
# d = {'c': 2, 'd': 1, 'b': 3, 'a': 5}
# print(d.items())
# print(sorted(d.items()))
# print(sorted(d.items(), key=lambda item: item[1]))
# print(dict(sorted(d.items(), key=lambda item: item[1], reverse=True)))
# print(dict(sorted(d.items(), key=lambda item: item[1], reverse=False)))

# ----------------------------------------------------------------------------------------------------------
# ##reduce()
# from functools import reduce
#
# # a = [1, 2, 3, 4]    # -->1234
# # def func1(x, y):
# #     return x * 10 + y
# # print(reduce(func1, a))
#
# # 将字符串转换为int型
# b = "123"
# c = {'1': 1, '2': 2, "3": 3}
# lis = list(map(lambda x: c[x], b))
# a = reduce(lambda x, y: x * 10 + y, list(map(lambda x: c[x], b)))
# print(a, type(a))

# --------------------------------------------------------------------------
# 检查用户名
import re

# username = input("input your name: ")
username = ['fa_', '34adf_', '_afdf22', 'fad342ad21213121sdfsdsd', 'fad342ad_']


def ree(x):
    return re.findall(r"^[a-zA-Z][\w ]{5,17}$", x)


print(list(filter(lambda item: len(item) != 0, map(ree, username))))
print()


# ---------------------------------------------------------------------------
# 匿名函数+reduce实现n的阶乘
from functools import reduce
print(reduce(lambda x, y: x * y, range(1, 3)))
print(reduce(lambda x, y: x * y, range(1, 4)))
print(reduce(lambda x, y: x * y, range(1, 5)))
print()

# ---------------------------------------------------------------------------
# filter函数：找出[1-100]能被3整除的数
print(list(filter(lambda item: item % 3 == 0, range(1, 101))))
print()

# ---------------------------------------------------------------------------
# map函数:求list[1-10]各项的平方
print(list(map(lambda item: item**2, range(1, 11))))
print()


# ---------------------------------------------------------------------------
# 将列表[1, 4, 5, 9]转换成'1459'字符串
a = [1, 4, 5, 9, 1]
b = {}
for i in a:
    b[i] = str(i)
print(b)
print(reduce(lambda x, y: x + y, list(map(lambda item: b[item], a))))
print(reduce(lambda x, y: str(x) + str(y), a))
print()


# ---------------------------------------------------------------------------
# 列表正负整数排序
list1 = [7, -8, 5, 4, 0, -2, -5]


def func(x):
    if x < 0:
        return abs(x) + 10
    else:
        return x


print(sorted(list1, key=func))
print()


# ---------------------------------------------------------------------------
# 字符串排序
s = "asbf234GDSdsf23"


def func1(x):
    if 48 <= ord(x) <= 57:
        if ord(x) % 2 == 0:
            return ord(x)
        else:
            return ord(x) - 10
    elif 97 <= ord(x) <= 122:
        return ord(x) - 100
    else:
        return ord(x) - 52


print(sorted(s, key=func1))
print(list(map(func1, sorted(s, key=func1))))
